//
//  CheckViewController.m
//  Qihuo
//
//  Created by ~Fighting~ on 13-10-20.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "GroupParentCheckViewController.h"
#import "HighLowerQuCheckViewController.h"
#import "CheckViewController.h"
#import "PickerBackgroundView.h"
#import "CheckXunjianCell.h"
#import "CarManagementCheckViewController.h"
#import "MieHuoQiCheckViewController.h"
#import "FireControlCheckViewController.h"
#import "StrongCheckViewController.h"
#import "JiFangCheckViewController.h"
#import "WeakCheckViewController.h"
#import "UPSCheckViewController.h"
#import "TransQICheckViewController.h"
#import "PeopleProjectCheckViewController.h"
#import "ElevatorCheckViewController.h"
#import "AHUCheckViewController.h"
#import "WLMMCheckViewController.h"
#import "CCTVCheckViewController.h"
#import "GreenHuaCheckViewController.h"
#import "F37CheckViewController.h"
#import "EveryDayCheckViewController.h"
#import "RecordCell.h"
#import "ProjectDeviceCheckViewController.h"
@interface CheckViewController ()
{
    UILabel *dateLabel;
    UITableView *readTable;
    PickerBackgroundView *pickGroundV;
    ASIFormDataRequest *obtainFloor;
    NSMutableArray *checkTimes;
    NSMutableArray *checkNames;
    NSMutableArray *checkAreas;
    NSMutableArray *isAbnomals;
}
@end

@implementation CheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"巡检记录查询";
    _sendCheckTime = @"";
    UIImage *lineImage = [UIImage imageNamed:@"黑边框"];
    UIImage *whiteimage = [UIImage imageNamed:@"底框-白.png"];
    UIImageView *MyView = [[UIImageView alloc]initWithFrame:CGRectMake((scrennWidth - whiteimage.size.width)/2, 150, whiteimage.size.width, whiteimage.size.height)];
    MyView.image = whiteimage;
    [MyView setUserInteractionEnabled:YES];
    [self.view addSubview:MyView];
    
    dateLabel = [UILabel new];
    NSDateFormatter *fom = [NSDateFormatter new];
    [fom setDateFormat:@"yyyy年MM月dd日"];
    dateLabel.text = [fom stringFromDate:[NSDate date]];
    //    [Appinfomation shareIstance].dateStr = dateLabel.text;
    dateLabel.font = [UIFont systemFontOfSize:35];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textColor = [UIColor whiteColor];
    CGSize size = [dateLabel.text sizeWithFont:dateLabel.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    dateLabel.frame = CGRectMake(33, (MyView.frame.origin.y - 44 - size.height)/2 + 44, size.width, size.height);
    [self.view addSubview:dateLabel];
    
    UIButton *calenderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *calenderImage = [UIImage imageNamed:@"日历按钮"];
    [calenderBtn setImage:calenderImage forState:UIControlStateNormal];
    [calenderBtn setFrame:CGRectMake(dateLabel.frame.origin.x + dateLabel.frame.size.width + interval111 * 2, (MyView.frame.origin.y - 44 -calenderImage.size.height)/2 + 44, calenderImage.size.width, calenderImage.size.height)];
    [calenderBtn addTarget:self action:@selector(pickDate1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:calenderBtn];
    
    pickGroundV = [[PickerBackgroundView alloc]initWithFrame:CGRectMake(0, 0, scrennWidth, screenHeight)];
    pickGroundV.userInteractionEnabled = YES;
    pickGroundV.butonDelegate = self;
    [pickGroundV canDate];
    pickGroundV.tag = 0;
    [self.view addSubview:pickGroundV];
    pickGroundV.hidden = YES;
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:lineImage];
    lineView.frame = CGRectMake((whiteimage.size.width - lineImage.size.width)/2, (whiteimage.size.height - lineImage.size.height)/2, lineImage.size.width, lineImage.size.height);
    lineView.userInteractionEnabled = YES;
    [MyView addSubview:lineView];
    
    readTable = [[UITableView alloc]initWithFrame:CGRectMake(2, 5, lineImage.size.width-6, lineImage.size.height-8) style:UITableViewStylePlain];
    readTable.dataSource = self;
    readTable.delegate = self;
    [readTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [lineView addSubview:readTable];
    [self addSearch];
    [self makeRecordView];
    
    checkNames = [NSMutableArray array];
    checkTimes = [NSMutableArray array];
    checkAreas = [NSMutableArray array];
    isAbnomals = [NSMutableArray array];
    _resignRecords = [NSArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSURL *queryUrl = [NSURL URLWithString:checkRecord];
    obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:[[Appinfomation shareIstance].loginDic valueForKey:@"departmentid"] forKey:@"departmentId"];
    [obtainFloor startAsynchronous];
    obtainFloor.delegate = self;
    [obtainFloor setDidFinishSelector:@selector(requestDone)];
    [obtainFloor setDidFailSelector:@selector(requestFail)];
    
}
- (void)makeRecordView
{
    _recordView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [_recordView setImage:[UIImage imageNamed:@"黑底1"]];
    _recordView.userInteractionEnabled = 1;
    [self.view addSubview:_recordView];
    
    UIImage *whiteBackGroundImage = [UIImage imageNamed:@"签到白底"];
    UIImageView *whiteImageView = [[UIImageView alloc]initWithFrame:CGRectMake((1024 - whiteBackGroundImage.size.width)/2, (768 - whiteBackGroundImage.size.height)/2, whiteBackGroundImage.size.width, whiteBackGroundImage.size.height)];
    [whiteImageView setImage:whiteBackGroundImage];
    whiteImageView.userInteractionEnabled = 1;
    [_recordView addSubview:whiteImageView];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *closeImage = [UIImage imageNamed:@"关闭"];
    [closeBtn setImage:closeImage forState:UIControlStateNormal];
    closeBtn.frame = CGRectMake(whiteBackGroundImage.size.width - closeImage.size.width - 20, 20, closeImage.size.width, closeImage.size.height);
    [whiteImageView addSubview:closeBtn];
    [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *firtLb = [UILabel new];
    firtLb.text = @"签 到 记 录";
    firtLb.font = [UIFont systemFontOfSize:30];
    CGSize mySz = [firtLb.text sizeWithFont:firtLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    firtLb.frame = CGRectMake((whiteBackGroundImage.size.width - mySz.width)/2, 40, mySz.width, mySz.height);
    [whiteImageView addSubview:firtLb];
    
    UIImage *firstImage = [UIImage imageNamed:@"签到记录框"];
    UIImageView *firstView = [[UIImageView alloc]initWithFrame:CGRectMake((whiteBackGroundImage.size.width - firstImage.size.width)/2, firtLb.frame.origin.y + 50, firstImage.size.width, firstImage.size.height)];
    [firstView setImage:firstImage];
    [whiteImageView addSubview:firstView];
    
    _startDate = [UILabel new];
    _startDate.text = @"1111111111";
    _startDate.font = [UIFont systemFontOfSize:25];
    mySz = [_startDate.text sizeWithFont:_startDate.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _startDate.frame = CGRectMake(180, 20, mySz.width, mySz.height);
    [firstView addSubview:_startDate];
    
    _startTime = [UILabel new];
    _startTime.text = @"11111111";
    _startTime.font = [UIFont systemFontOfSize:25];
    mySz = [_startTime.text sizeWithFont:_startTime.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _startTime.frame = CGRectMake(400, _startDate.frame.origin.y, mySz.width, mySz.height);
    [firstView addSubview:_startTime];
    
    _endDate = [UILabel new];
    _endDate.text = @"1111111111";
    _endDate.font = [UIFont systemFontOfSize:25];
    mySz = [_endDate.text sizeWithFont:_endDate.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _endDate.frame = CGRectMake(_startDate.frame.origin.x, 80, mySz.width, mySz.height);
    [firstView addSubview:_endDate];
    
    _endTime = [UILabel new];
    _endTime.text = @"11111111";
    _endTime.font = [UIFont systemFontOfSize:25];
    mySz = [_endTime.text sizeWithFont:_endTime.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _endTime.frame = CGRectMake(_startTime.frame.origin.x, _endDate.frame.origin.y, mySz.width, mySz.height);
    [firstView addSubview:_endTime];
    
    _resumeTime = [UILabel new];
    _resumeTime.text = @"123";
    _resumeTime.font = [UIFont systemFontOfSize:25];
    mySz = [_resumeTime.text sizeWithFont:_resumeTime.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    _resumeTime.frame = CGRectMake(320, 135, mySz.width, mySz.height);
    [firstView addSubview:_resumeTime];
    
    UILabel *secondLb = [UILabel new];
    secondLb.text = @"签到节点记录";
    secondLb.font = [UIFont systemFontOfSize:30];
    mySz = [secondLb.text sizeWithFont:secondLb.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
    secondLb.frame = CGRectMake((whiteBackGroundImage.size.width - mySz.width)/2, firstView.frame.origin.y + firstImage.size.height + 50, mySz.width, mySz.height);
    [whiteImageView addSubview:secondLb];
    
    UIImage *secondImage = [UIImage imageNamed:@"12"];
    UIImageView *secondView = [[UIImageView alloc]initWithFrame:CGRectMake((whiteBackGroundImage.size.width - secondImage.size.width)/2, secondLb.frame.origin.y + 50, secondImage.size.width, secondImage.size.height)];
    [secondView setImage:secondImage];
    [whiteImageView addSubview:secondView];
    
    _checkTableView = [[UITableView alloc]initWithFrame:CGRectMake(secondView.frame.origin.x, secondView.frame.origin.y + secondView.frame.size.height, secondImage.size.width, secondImage.size.height * 4)];
    _checkTableView.tag = 250;
    _checkTableView.delegate = self;
    _checkTableView.dataSource = self;
    UIImageView *tableBackGroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"22"]];
    [_checkTableView setBackgroundView:tableBackGroundView];
    [whiteImageView addSubview:_checkTableView];
    
    _recordView.hidden = 1;
}
#pragma mark - UITableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _checkTableView) {
        return _resignRecords.count;
    }else{
        return checkTimes.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"myCell";
    if (tableView.tag == 250)
    {
        RecordCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[RecordCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        UIImageView *cellView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"22"]];
        cell.personLb.text = [[_resignRecords objectAtIndex:indexPath.row]valueForKey:@"userName"];
        cell.timeLb.text = [[_resignRecords objectAtIndex:indexPath.row]valueForKey:@"regTime"];
        cell.placeLb.text = [NSString stringWithFormat:@"%@",[[_resignRecords objectAtIndex:indexPath.row]valueForKey:@"floorId"]];
        [cell setBackgroundView:cellView];
        return cell;
    }
    else
    {
        CheckXunjianCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[CheckXunjianCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
//        if ([[isAbnomals objectAtIndex:indexPath.row]isEqualToString:@"1"])
//        {
//            cell.nameLabel.textColor = [UIColor redColor];
//            cell.timeLabel.textColor = [UIColor redColor];
//        }
        cell.nameLabel.text = [checkNames objectAtIndex:indexPath.row];
        cell.timeLabel.text = [checkTimes objectAtIndex:indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        self.sendCheckTime = [checkTimes objectAtIndex:indexPath.row];
        self.checkAreaNumber = [[checkAreas objectAtIndex:indexPath.row]integerValue];
        cell.delegate = self;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 250)
    {
        return [UIImage imageNamed:@"22"].size.height;
    }
    else
    {
        return 40;
    }
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == readTable)
    {
        self.sendCheckTime = [checkTimes objectAtIndex:indexPath.row];
        self.checkAreaNumber = [[checkAreas objectAtIndex:indexPath.row]integerValue];
        switch (self.checkAreaNumber)
        {
            case 1:
            {
                HighLowerQuCheckViewController *highV = [HighLowerQuCheckViewController new];
                [self.navigationController pushViewController:highV animated:YES];
                highV.delegate = self;
            }
                break;
            case 2:
            {
                HighLowerQuCheckViewController *LowerV = [HighLowerQuCheckViewController new];
                [self.navigationController pushViewController:LowerV animated:YES];
                LowerV.delegate = self;
            }
                break;
            case 3:
            {
                CarManagementCheckViewController *carV = [CarManagementCheckViewController new];
                [self.navigationController pushViewController:carV animated:YES];
                carV.delegate = self;
            }
                break;
            case 4:
            {
                MieHuoQiCheckViewController *miehuoV = [MieHuoQiCheckViewController new];
                [self.navigationController pushViewController:miehuoV animated:YES];
                miehuoV.delegate = self;
            }
                break;
            case 5:
            {
                MieHuoQiCheckViewController *miehuoV = [MieHuoQiCheckViewController new];
                [self.navigationController pushViewController:miehuoV animated:YES];
                miehuoV.delegate = self;
            }
                break;
            case 6:
            {
                FireControlCheckViewController *fireV = [FireControlCheckViewController new];
                [self.navigationController pushViewController:fireV animated:YES];
                fireV.delegate = self;
            }
                break;
            case 7:
            {
                WeakCheckViewController *weakV = [WeakCheckViewController new];
                [self.navigationController pushViewController:weakV animated:YES];
                weakV.delegate = self;
            }
                break;
            case 8:
            {
                WeakCheckViewController *weakV = [WeakCheckViewController new];
                [self.navigationController pushViewController:weakV animated:YES];
                weakV.delegate = self;
            }
                break;
            case 9:
            {
                UPSCheckViewController *UPSV = [UPSCheckViewController new];
                [self.navigationController pushViewController:UPSV animated:YES];
                UPSV.delegate = self;
            }
                break;
            case 10:
            {
                TransQICheckViewController *tranV = [TransQICheckViewController new];
                tranV.delegate = self;
                [self.navigationController pushViewController:tranV animated:YES];
            }
                break;
            case 11:
            {
                StrongCheckViewController *strV = [StrongCheckViewController new];
                strV.delegate = self;
                [self.navigationController pushViewController:strV animated:YES];
            }
                break;
            case 12:
            {
                StrongCheckViewController *strV = [StrongCheckViewController new];
                strV.delegate = self;
                [self.navigationController pushViewController:strV animated:YES];
            }
                break;
            case 13://机房电柜
            {
                JiFangCheckViewController *JFV = [JiFangCheckViewController new];
                JFV.delegate = self;
                [self.navigationController pushViewController:JFV animated:YES];
            }
                break;
            case 14://工程设备
            {
                ProjectDeviceCheckViewController *pro = [ProjectDeviceCheckViewController new];
                [self.navigationController pushViewController:pro animated:YES];
                pro.delegate = self;
            }
                break;
            case 15:
            {
                PeopleProjectCheckViewController *peopV = [PeopleProjectCheckViewController new];
                peopV.delegate = self;
                [self.navigationController pushViewController:peopV animated:YES];
            }
                break;
            case 16://AHU
            {
                AHUCheckViewController *AHUV = [AHUCheckViewController new];
                AHUV.delegate = self;
                [self.navigationController pushViewController:AHUV animated:YES];
            }
                break;
            case 17://外立面幕墙建筑
            {
                WLMMCheckViewController *WLMMV = [WLMMCheckViewController new];
                WLMMV.delegate = self;
                [self.navigationController pushViewController:WLMMV animated:YES];
            }
                break;
            case 18://外立面幕墙qihou
            {
                WLMMCheckViewController *WLMMV = [WLMMCheckViewController new];
                WLMMV.delegate = self;
                [self.navigationController pushViewController:WLMMV animated:YES];
            }
                break;
            case 19:
            {
                ElevatorCheckViewController *eleV = [ElevatorCheckViewController new];
                eleV.delegate = self;
                [self.navigationController pushViewController:eleV animated:YES];
            }
                break;
            case 20://CCTV
            {
                CCTVCheckViewController *CCTV = [CCTVCheckViewController new];
                CCTV.delegate = self;
                [self.navigationController pushViewController:CCTV animated:YES];
            }
                break;
            case 21:
            {
                EveryDayCheckViewController *everyDay = [EveryDayCheckViewController new];
                everyDay.delegate = self;
                [self.navigationController pushViewController:everyDay animated:YES];
            }
                break;
            case 22:
            {
                
            }
                break;
            case 23:
            {
                F37CheckViewController *F37V = [F37CheckViewController new];
                [self.navigationController pushViewController:F37V animated:YES];
                F37V.delegate = self;
            }
                break;
            case 24://外围绿化
            {
                GreenHuaCheckViewController *greV = [GreenHuaCheckViewController new];
                [self.navigationController pushViewController:greV animated:YES];
                greV.delegate = self;
            }
                break;
            case 25:
            {
                
            }
                break;
            case 26:
            {
                
            }
                break;
            case 27:
            {
                
            }
                break;
            case 28:
            {
                
            }
                break;
            case 29:
            {
                
            }
                break;
            case 30:
            {
                
            }
                break;
                
            default:
                break;
        }
    }
    else if (tableView.tag == 250)
    {
        
    }
}
#pragma mark - buttonDelegate
- (void)sureOrCancelDelegate:(UIButton*)btn tag:(NSInteger)tag
{
    if (btn.tag == 0)
    {
        if (pickGroundV.timeStr.length)
        {
            dateLabel.text = pickGroundV.timeStr ;
        }
        else
        {
            NSDateFormatter *fom = [NSDateFormatter new];
            [fom setDateFormat:@"yyyy年MM月dd日"];
            dateLabel.text = [fom stringFromDate:[NSDate date]];
        }
    }
    else
    {
        
    }
    self.navigationController.navigationBar.hidden = 0;
    pickGroundV.hidden = YES;
}
#pragma mark - button action
- (void)closeAction
{
    self.recordView.hidden = YES;
    self.navigationController.navigationBar.hidden = 0;
}
- (void)pickDate1
{
    pickGroundV.hidden = NO;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)searchInfo:(id)sender
{
    NSURL *queryUrl = [NSURL URLWithString:checkRecord];
    obtainFloor = [ASIFormDataRequest requestWithURL:queryUrl];
    [obtainFloor setPostValue:[[Appinfomation shareIstance].loginDic valueForKey:@"departmentid"] forKey:@"departmentId"];
    [obtainFloor setPostValue:[self.seachBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:@"name"];
    NSString *yearStr = [dateLabel.text substringToIndex:4];
    NSString *monthStr = [dateLabel.text substringWithRange:NSMakeRange(5, 2)];
    NSString *dayStr = [dateLabel.text substringWithRange:NSMakeRange(8, 2)];
    [obtainFloor setPostValue:[NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dayStr] forKey:@"date"];
    [obtainFloor startSynchronous];
    
    NSString *response = [obtainFloor responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSArray *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog(@"查询%@",queryDataArr);
    [checkNames removeAllObjects];
    [checkTimes removeAllObjects];
    [isAbnomals removeAllObjects];
    for (NSDictionary *dic in queryDataArr)
    {
        [isAbnomals addObject:[dic objectForKey:@"isNormal"]];
        [checkNames addObject:[dic objectForKey:@"name"]];
        [checkTimes addObject:[dic objectForKey:@"time"]];
    }
    [readTable reloadData];
    [[[UIApplication sharedApplication]keyWindow] endEditing:YES];
}
#pragma mark - request
- (void)requestDone
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [obtainFloor responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSArray *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog(@"查询%@",queryDataArr);
    //    for (int i = 0; i < queryDataArr.count ; i++)
    //    {
    //        NSDictionary *dic = [queryDataArr objectAtIndex:i];
    //        [checkNames addObject:[dic valueForKey:@"name"]];
    //        [checkTimes addObject:[dic valueForKey:@"time"]];
    //    }
    for (NSDictionary *dic in queryDataArr) {
        [checkNames addObject:[dic objectForKey:@"name"]];
        [checkTimes addObject:[dic objectForKey:@"time"]];
        [checkAreas addObject:[dic objectForKey:@"checkArea"]];
    }
    [readTable reloadData];
    
    
}
- (void)requestFail
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"请求数据失败！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [failAlert show];
}
#pragma mark - end
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
